<?php
class usercontrol extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('moduser');
	}
	
	function index()
	{
		$config['base_url'] = base_url().'usercontrol/index/';
		$config['total_rows'] = $this->db->count_all('usercontrol');
		$config['per_page'] = 15;
		$config['uri_segment'] = 3;
		
		$config['full_tag_open'] = '<div class="pagination"><ul>';
		$config['full_tag_close'] = '</ul></div><!--pagination-->';

		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page">';
		$config['first_tag_close'] = '</li>';

		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page">';
		$config['last_tag_close'] = '</li>';

		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page">';
		$config['next_tag_close'] = '</li>';

		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page">';
		$config['prev_tag_close'] = '</li>';

		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';

		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';
		
		$this->pagination->initialize($config);
		
		$token = $this->session->userdata('token');
		if($token){
			$data['results'] = $this->moduser->getList($config['per_page'],$this->uri->segment(3));
			$this->load->view('usercontrol/list',$data);
		}else{
			redirect('login');
		}
	}
    
	function add(){
		$data['mode'] = "Tambah Pengguna";
		$data['action'] = "save";
		$this->load->view('usercontrol/form',$data);
	}
	
	function edit($id_user){
		$data['mode'] = "Ubah Pengguna";
		$data['action'] = "update";
		$data['detail'] = $this->moduser->detail($id_user);
		$this->load->view('usercontrol/form',$data);
	}
	
	function save()
	{
		$id_user = $this->input->post('id_user');
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$email_user = $this->input->post('email_user');
		$enable = $this->input->post('enable');
		
		$submit = $this->input->post('submit');	
		if($submit)
		{
			$this->moduser->setData($id_user,$username,$password,$email_user,$enable);
			$this->moduser->create();
			$this->session->set_flashdata('success', true);
			redirect('usercontrol');
		}
		$this->session->set_flashdata('error', true);
		redirect('usercontrol');
	}
	
	function update()
	{
		$id_user = $this->input->post('id_user');
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$email_user = $this->input->post('email_user');
		$enable = $this->input->post('enable');
		
		$submit = $this->input->post('submit');	
		if ($submit)
		{
			$this->moduser->setData($id_user,$username,$password,$email_user,$enable);
			$this->moduser->update($id_user);
			$this->session->set_flashdata('success', true);
			redirect('usercontrol');
		}
		$this->session->set_flashdata('error', true);
		redirect('usercontrol');
	}
	
	function delete($id_user)
	{
		if ($this->moduser->remove($id_user)){
			$this->session->set_flashdata('delete', true);
			redirect('usercontrol','refresh');
		}
		$this->session->set_flashdata('error', true);
		redirect('usercontrol');
	}	
	
	function searchData(){
		$name = $this->input->post('name');
		$data['results'] = $this->moduser->getListSearch($name);
		$this->load->view('usercontrol/list',$data);
	}
}
?>